package router

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"perm-guard/handlers"
)

func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
func Init() *gin.Engine {
	r := gin.Default()
	r.Use(Cors())
	r.Static("/static", "./static")
	r.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "hello World!")
	})
	r.POST("/login", handlers.Login)              //用户登录
	r.POST("/userRoleAdd", handlers.UserRoleAdd)  //用户角色关联
	r.POST("/roleNodeAdd", handlers.RoleNodeAdd)  //角色权限关联
	r.GET("/userRoleList", handlers.UserRoleList) //用户角色查询
	r.GET("/roleNodeList", handlers.RoleNodeList) //角色权限查询
	r.GET("/userList", handlers.UserList)         //用户列表
	r.GET("/userDetail", handlers.UserDetail)     //用户详情
	/////////////////////////车辆订单////////////////
	r.POST("/logins", handlers.MoneyLogin)       //用户登录
	r.POST("/add", handlers.Add)                 //添加
	r.POST("/carorderAdd", handlers.CarOrderAdd) //交易车辆添加
	r.GET("/carorderList", handlers.CarOrderList)
	r.GET("/carOrderDetail", handlers.CarOrderDetail)
	/////////////////////车辆合同//////////////
	r.GET("/carConList", handlers.CarConList)
	r.GET("/carConDetail", handlers.CarConDetail)
	//////////////////////车辆数据信息///////////////////
	r.GET("/carTypeList", handlers.CarTypeList)
	r.GET("/carTypeDetail", handlers.CarTypeDetail)
	////////////////物流配送/////////////////
	r.POST("/peopleAdd", handlers.PeopleAdd) //配送员信息添加
	r.POST("/orderAdd", handlers.OrderAdd)
	r.GET("/peopleList", handlers.PeopleList)
	r.GET("/peopleorderDetail", handlers.PeopleOrderDetail)
	return r
}
